var confirmEdit = {};
var ConfirmEdit = function () {
    //初始化
    this.init = function () {
        layui.use(['form'], function () {
            confirmEdit.form = layui.form;
        });
        if ($("#standby") == "1") {
            $(".invoiceCode").show();
            $(".invoiceCode").find("input").attr("lay-verify", "required");
            $(".invoiceNo").show();
            $(".invoiceNo").find("input").attr("lay-verify", "required");
        } else {
            $(".invoiceCode").hide();
            $(".invoiceCode").find("input").removeAttr("lay-verify");
            $(".invoiceNo").hide();
            $(".invoiceNo").find("input").removeAttr("lay-verify");
        }
        //初始化数据字典
        if ($("#tranType").val() == "1") {//铁路运输
            //发出站
            confirmEdit.dictMap("aStation", this.trim($("#aStation").text()));
            //到站
            confirmEdit.dictMap("arriveStation",this.trim($("#arriveStation").text()));
            //到站承运
            confirmEdit.dictMap("standby3", this.trim($("#standby3").text()));
        } else if ($("#tranType").val() == "2") {//水路运输
            $("#goodsWere").text("提单号");
            $("#aStationText").text("发出港：");
            $("#arriveStationText").text("到港：");
            //发出港
            confirmEdit.dictMap("aStation", this.trim($("#aStation").text()));
            //到港
            confirmEdit.dictMap("arriveStation", this.trim($("#arriveStation").text()));
        } else if ($("#tranType").val() == "3") {//公路运输
            $("#arriveStationText").text("卸货地点：");
            //发出站
            confirmEdit.dictMap("aStation", this.trim($("#aStation").text()));
            //到站
            confirmEdit.dictMap("arriveStation", this.trim($("#arriveStation").text()));
        }
        //业务类型
        confirmEdit.dictMap("businessTypes",this.trim($("#businessTypes").text()));
        //品名
        confirmEdit.dictMap("ofTheGoods",  this.trim($("#ofTheGoods").text()));
        //箱型
        confirmEdit.dictMap("aBox", this.trim($("#aBox").text()));
        //发货单位
        confirmEdit.dictMap("deliveryUnit", this.trim($("#deliveryUnit").text()));
        //装货地点
        confirmEdit.dictMap("standby1",  this.trim($("#standby1").text()));
        //收货人
        //confirmEdit.dictMap("receivingUnit", this.trim($("#receivingUnit").text()));
        //结算人
        confirmEdit.dictMap("settlementPeople", this.trim($("#settlementPeople").text()));
        //议价号
        confirmEdit.dictMap("bargaining", this.trim($("#bargaining").text()));
    };
    /**
     *
     * @param costType  费用类型
     * @param receivingUnit  收款单位
     * @param amountPayable  应付金额
     * @param realPayTotalAmount  实付金额
     * @param note   备注
     */
    this.addRow = function (costType, receivingUnit, amountPayable, realPayTotalAmount, note) {
        var rowHtml = `<tr>
                        <td>
                            ` + confirmEdit.joinSelect(costType) + `
                        </td>
                        <td>
                            ` + (amountPayable ? amountPayable : '') + `
                        </td>
                        <td>
                            ` + (realPayTotalAmount ? realPayTotalAmount : '') + `
                        </td>
                        <td>
                         ` + confirmEdit.joinSelect(receivingUnit) + `
                        </td>
                        <td>
                           ` + (note ? note : '') + `
                        </td>
                    </tr>`;
        $("#row").append(rowHtml);
        confirmEdit.form.render("select");
        layuiFunciton.moneyValidation(".money");
    };
    //计算金额
    this.calculationAmount = function () {
        var totalCosts = 0.00;
        var totalCostsList = $(".totalCosts");
        for (var i = 0; i < totalCostsList.length; i++) {
            var value = totalCostsList.eq(i).val();
            totalCosts = (Number(totalCosts) + Number(value)).toFixed(2);
        }
        $("#totalCosts").val(totalCosts);
        $("#totalCostsHtml").html(totalCosts);
    };
    //拼接Select
    this.joinSelect = function (valCode) {
        var dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
        if(valCode){
            if(dictMap[valCode]){
                return dictMap[valCode];
            }
        }
        return "";
    };
    //查询费用明细
    this.findConditions = function () {
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/wb/schedule/types/wbScheduleTypes',
            method: 'get',
            maskState: false,
            data: {
                orderCode: $("#orderCode").val(),
                states: 0
            },
            success: function (data) {
                data.forEach(function (obj, index) {
                    confirmEdit.addRow(obj.costType, obj.receivingUnit, obj.amountPayable, obj.amountRealPay, obj.note);
                });
            }
        });
    };
    this.trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    this.event = function () {
        //是否已开发票
        layuiFunciton.filter({
            types: 'form',   //   注：仅支持 from 、table 两种类型  监听 table的 tools
            filterTypes: 'checkbox',   //
            filterName: 'standby',
            returnData: function (obj) {
                if (obj.elem.checked) {
                    $(".invoiceCode").show();
                    $(".invoiceCode").find("input").attr("lay-verify", "required");
                    $(".invoiceNo").show();
                    $(".invoiceNo").find("input").attr("lay-verify", "required");
                } else {
                    $(".invoiceCode").hide();
                    $(".invoiceCode").find("input").removeAttr("lay-verify");
                    $(".invoiceNo").hide();
                    $(".invoiceNo").find("input").removeAttr("lay-verify");
                }
            }
        });
        //保存
        layuiFunciton.formSubmit({
            filterName: "editSubmit",
            url: layuiFunciton.URL + '/wb/shipping/order/updateWbShippingOrder',
            formId: "editForm",
            success: function (returnData) {
                if (returnData > 0) {
                    layuiFunciton.success('操作成功！', function () {
                        layuiFunciton.closeOpen();
                    })
                } else {
                    layuiFunciton.warn('操作失败!');
                }
            }
        });
    }
    this.dictMap = function (item,valCode) {
        var dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
        if(dictMap[valCode]){
            $("#"+item).text(dictMap[valCode]);
        }else{
            $("#"+item).text("");
        }
    }
};
function pageReady() {
    confirmEdit = new ConfirmEdit();
    confirmEdit.findConditions();
    confirmEdit.init();
    confirmEdit.event();
}